package cn.jdy.plugins.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.safe.SafeUtil;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.plugins.entity.Dealer;
import cn.jdy.plugins.service.DealerService;

/**
 * 后台经销商管理
 *
 * 
 */
@Controller
@RequestMapping("/dealer")
public class DealerCtrl extends BaseCtrl {
	@Autowired
	private DealerService dealerService;


	/**
	 * 初始化
	 *
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list() {
		return new ModelAndView("/dealer/dealerList.jsp");
	}
	
     /**
	 * 查询
	 *
	 * @param member
	 * @return
	 */
	@RequestMapping("/queryByPage")
	@ResponseBody
	public Map<String, Object> queryByPage(Dealer dealer, String fromDate, String toDate) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			jsonMap.put("dataList", dealerService.query(dealer, fromDate, toDate));
			jsonMap.put("iTotalDisplayRecords", dealerService.count(dealer, fromDate, toDate));
			jsonMap.put("iTotalRecords", dealerService.count());
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "查询错误", e);
		}
		return jsonMap;
	}

	
	@RequestMapping("/add")
	public ModelAndView add() {
		return new ModelAndView("/dealer/dealerEdit.jsp");
	}
	
	
	@RequestMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable String id) {
		ModelAndView mv = new ModelAndView();
		Dealer dealer = this.dealerService.find(id);
		if(dealer != null) {
			mv.setViewName("/dealer/dealerEdit.jsp");
			mv.addObject("data", dealer);
		}
		return mv;
	}

	@RequestMapping("/oper")
	@ResponseBody
	public Map<String, Object> oper(String id, int type, String value) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			String result = this.dealerService.updateBatch(id, type, value);
			jsonMap.put("statusCode", 200);
			if(result != null) jsonMap.put("info", result);
		} catch(Exception e) {
			jsonMap.put("statusCode", 500);
			BaseLog.e(getClass(), "经销商批量更新异常！", e);
		}
		return jsonMap;
	}

	@RequestMapping("/save")
	@ResponseBody
	public Map<String, Object> save(Dealer entity) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			if(entity.getId() == null || entity.getId().length()==0) {
				entity.setId(null);
				entity.setPhoto("/images/no-img_mid_.jpg");
				entity.setCreateTime(DateUtil.getCurrentTime());
				entity.setPassword(SafeUtil.md5(entity.getUserName()).substring(8, 16));
				this.dealerService.save(entity);
			}else
				this.dealerService.updateWithValidator(entity);
			jsonMap.put("statusCode", 200);
		} catch(Exception e) {
			jsonMap.put("statusCode", 500);
			BaseLog.e(getClass(), "经销商批量更新异常！", e);
		}
		return jsonMap;
	}

	/**
	 * 下载Excel
	 *
	 * @return
	 */
	@RequestMapping("/downExcel")
	public void downExcel(Dealer dealer, HttpServletResponse response) {
		List<Dealer> list = this.dealerService.query(dealer);
		String name = "个人经销商"+DateUtil.getCurrentDate4();
		String[] titles = new String[]{"公司名称", "公司地址", "所属省","所属市","所属区域","手机号", "负责人", "状态"};
		String[] fieldNames = new String[]{"companyName", "companyLocation", "province", "city", "district", "mobile","userName","status"};
		super.downExcel(list, titles, fieldNames, name, response);
	}
}